package com.example.shoppingdemo.nslcommodity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.javabean.BrandModel;

import java.util.List;

public class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.BrandHolder>{
    private List<BrandModel>brandModels;
    private Context context;
    private onBrandParticulars onBrandParticulars;
    public BrandAdapter(Context context) {
        this.context = context;
    }

    public void setBrandModels(List<BrandModel> brandModels) {
        this.brandModels = brandModels;
    }

    public void setOnBrandParticulars(BrandAdapter.onBrandParticulars onBrandParticulars) {
        this.onBrandParticulars = onBrandParticulars;
    }

    @NonNull
    @Override
    public BrandHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BrandHolder holder=new BrandHolder(LayoutInflater.from(context).inflate(R.layout.brand_item,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BrandAdapter.BrandHolder holder, int position) {
        holder.brand.setText(brandModels.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return null==brandModels?0:brandModels.size();
    }

    class BrandHolder extends RecyclerView.ViewHolder{
            TextView brand;
        public BrandHolder(@NonNull View itemView) {
            super(itemView);
            brand=itemView.findViewById(R.id.text_brand);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBrandParticulars.onBrandResult(BrandHolder.this.getAdapterPosition());
                }
            });
        }
    }
    public interface onBrandParticulars{
        void onBrandResult(int position);
    }
}
